module EasyForm
  module Components
    module Readonly
      def readonly(wrapper_options = nil)
        if readonly_attribute? && !has_readonly?
          input_html_options[:readonly] ||= true
          input_html_classes << :readonly
        end
        nil
      end

      private
      def readonly_attribute?
        klass = object.class
        klass.respond_to?(:readonly_attributes) &&
            object.persisted? &&
            klass.readonly_attributes.include?(attribute_name.to_s)
      end
    end
  end
end